package com.tensquare.search.service;

import com.tensquare.search.dao.SearchDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;

/**
 * @Author:zimmor
 * @Date: 2021/3/6 16:56
 * @Description
 * @version: 1.0.0
 */
@Service
@Transactional
public class SearchService {
    @Autowired
    private SearchDao searchDao;

    @Autowired
    private IdWorker idWorker;

    public void add(Article article) {
        article.setId(idWorker.nextId()+"");
        searchDao.save(article);
    }

    /**
     * 根据关键字搜索title和content
     * @param keyword
     * @param page
     * @param size
     * @return
     */
    public Page<Article> findByKeyWord(String keyword, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        return searchDao.findByTitleOrContentLike(keyword, keyword, pageable);
    }
}
